<?php defined('BASEPATH') OR exit('No direct script access allowed');

//微信分享接口类

class Wechatshare {

    private $appId;
    private $appSecret;
    private $_CI;

    public function __construct() {
        $_CI = &get_instance();
        $this->_CI = $_CI;
        $this->_CI->load->driver('cache');
        $this->appId = $appId = 'wx8890443885bccba8';
        $this->appSecret = $appSecret = '695eae8bc33a2fd425f96f17ea0fde7d';
    }

    public function getSignPackage() {
        $jsapiTicket = $this->getJsApiTicket();
        $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $timestamp = time();
        $nonceStr = $this->createNonceStr();

        // 这里参数的顺序要按照 key 值 ASCII 码升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

        $signature = sha1($string);

        $signPackage = array(
            "appId"     => $this->appId,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $string
        );
        return $signPackage; 
    }

    private function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    private function getJsApiTicket() {

        // jsapi_ticket 全局存储与更新
        $jsapi_ticket = $this->_CI->cache->redis->get('jsapi_ticket');
        if(isset($jsapi_ticket) && !empty($jsapi_ticket)) {
                $ticket = $jsapi_ticket;
        } else {
            $accessToken = $this->getAccessToken();
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
            $res = json_decode($this->httpGet($url));
            $ticket = $res->ticket;
            if ($ticket) {
              $this->_CI->cache->redis->save('jsapi_ticket', $ticket, 7000);//存入redis;
            }
    }
        return $ticket;
    }

    private function getAccessToken() {
        // access_token 全局存储与更新
        $access_token = $this->_CI->cache->redis->get('access_token');
        if(isset($access_token) && !empty($access_token)) {
                $token = $access_token;
        } else {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
            $res = json_decode($this->httpGet($url));
            $token = $res->access_token;
            if ($token) {
                $this->_CI->cache->redis->save('access_token', $token, 7000);//存入redis;
              }
        }
        return $token;
    }


    private function httpGet($url){
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL,$url);
    	// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    	// curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    	$temp = curl_exec($ch);
    	curl_close($ch);
    	return $temp;
    }
}